const app = getApp()
const util = require('../../utils/util.js')
Page({
  /**
   * 页面的初始数据
   */
  data: {
    host:app.globalData.host,
    login:'',
    modalName:'',
    content:'',
    comments:[],
    answer:'',
  },
  showModal:function(e){
    this.setData({
      modalName: 'modal1',
      
    })
  },
  showModal_3:function(e){
    console.log(e)
    this.setData({
      modalName: 'answer',
      answer_id:e.target.dataset.id
    })
  },
  showModal_1:function(e){
    this.setData({
      modalName: 'nofollow',
    })
  },
  hideModal:function(){
    this.setData({
      modalName: '',
      content:'',
    })
  },


  ViewImage(e) {
    var array = this.data.data.image_array;
    for(var i = 0;i<array.length;i++){
      array[i] = this.data.host+array[i];
    }
    wx.previewImage({
      urls: array,
      current: e.currentTarget.dataset.url
    });
  },

  commentInput:function(e){
    this.setData({
      content: e.detail.value
    })
  },

  answerInput:function(e){
    this.setData({
      answer: e.detail.value
    })
  },

  answer:function(e){
    var that = this;
    if(that.data.answer == '' || that.data.answer == undefined){
      util.error('内容不能为空'); return;
    }
    util.request('comment/answer', {answer:that.data.answer,id:that.data.answer_id}, function (data) {
      if(data.code == 1){
        that.loadData(that.data.id);
        that.setData({
          modalName:'',
          answer:'',
        })
      }else{
        util.fail(data.msg,that);
      }
    });
  },


  comment:function(e){
    var that = this;
    if(that.data.content == '' || that.data.content == undefined){
      util.error('内容不能为空'); return;
    }
    util.request('comment/comment', {content:that.data.content,related_id:that.data.id}, function (data) {
      if(data.code == 1){
        that.loadData(that.data.id);
        that.setData({
          modalName:'',
          content:'',
        })
      }else{
        util.fail(data.msg,that);
      }
    });
  },



    /**
   * 取消关注
   * @param {*} e 
   */
  nofollow:function(e){
    var that = this;
    util.request('follow/delete', {type:1,parent_id:that.data.data.parent_id}, function (data) {
      console.log(data);
      if(data.code == 1){
        that.loadData(that.data.id);
        that.hideModal();
        wx.showToast({
          title: '操作成功',
          icon: 'success',
          duration: 1500
        })
      }else{
        util.fail(data.msg,that);
      }
    });
  },
  /**
   * 点赞/取消点赞
   * @param {*} e 
   */
  thumb:function(e){
    var that = this;
    util.request('thumb/save', {type:3,related_id:that.data.id}, function (data) {
      if(data.code == 1){
        that.loadData(that.data.id);
      }else{
        util.fail(data.msg,that);
      }
    });
  },

    /**
   * 点赞/取消点赞 留言
   * @param {*} e 
   */
  thumb_comment:function(e){
    var that = this;
    util.request('thumb/save', {type:4,related_id:e.currentTarget.dataset.id}, function (data) {
      if(data.code == 1){
        that.loadData(that.data.id);
      }else{
        util.fail(data.msg,that);
      }
    });
  },

    /**
   * 收藏/取消收藏
   * @param {*} e 
   */
  collect:function(e){
    var that = this;
    util.request('collect/save', {related_id:that.data.id}, function (data) {
      if(data.code == 1){
        that.loadData(that.data.id);
      }else{
        util.fail(data.msg,that);
      }
    });
  },

    /**
   * 关注
   * @param {*} e 
   */
  follow:function(e){
    var data = e.target.dataset, parent_id =data.parent_id,child_id = data.child_id;
    var that = this;
    util.request('follow/add', {type:1,parent_id:parent_id,child_id:child_id}, function (data) {
      console.log(data);
      if(data.code == 1){
        that.loadData(that.data.id);
        wx.showToast({
          title: '关注成功',
          icon: 'success',
          duration: 1500
        })
      }else{
        util.fail(data.msg,that);
      }
    });
  },

  loadData:function(id){
    var that = this;
    util.request('share/detail', {id:id}, function (data) {
      if(data.code == 1){
        that.setData({
          data:data.data.data,
          comments:data.data.comment,
        });
        console.log(that.data.comments)

      }else{
        util.error(data.msg);
      }
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (e) {
    var id = e.id,that = this;
    console.log(app.globalData.parent)
    that.setData({
      id:id,
      parent_id:app.globalData.parent.id,
    });
    that.loadData(id);
    util.request('article/visit', {id:e.id}, function (data) {
      console.log(data);
    });
    if(e.message_id){
      util.request('message/read', {id:e.message_id}, function (data) {
        console.log(data);
      });
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      login:app.globalData.login,
    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})